﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zeny.Data.Domain.basedata;
using System.Collections;
using Zeny.Business.ExpSample.Service.Impl;
using Common.Logging;
using Zeny.Business.ExpSample.Service;
using Zeny.Basedata.Sync;

namespace Zeny.Basedata.Service
{
    public class DepartmentHelper : IDepartmentHelper, ICacheSync
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(DepartmentHelper));

        public IDepartmentService departmentService { get; set; }

        Dictionary<string, Department> departmentMap;

        private string rootDepartmentCode = "001";
        private int maxCheckDepartmentLevel = 10;

        public Department getDepartmentByCode(String departmentCode)
        {
            return departmentMap[departmentCode];
        }

        public bool isParentDepartment(Department parentDepartment, Department childDepartment)
        {
            string checkParentDepartmentCode = parentDepartment.departCode;
            string childParentDepartmentCode = childDepartment.parentCode;

            if(checkParentDepartmentCode.Equals(childParentDepartmentCode))
            {
                return true;
            }

            if (rootDepartmentCode.Equals(checkParentDepartmentCode))
            {
                return false;
            }

            //控制比较次数，避免由于数据错误导致的死循环
            int checkLevelCount = 0;
            while (!rootDepartmentCode.Equals(childParentDepartmentCode) && (++checkLevelCount < maxCheckDepartmentLevel))
            {
                Department childParentDepartment = departmentMap[childParentDepartmentCode];
                if (childDepartment != null)
                {
                    childParentDepartmentCode = childParentDepartment.parentCode;

                    if (checkParentDepartmentCode.Equals(childParentDepartmentCode))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        public ICollection<Department> getChildDepartmentList(Department department)
        {
            ICollection<Department> childDepartmentList = new LinkedList<Department>();

            string parentDepartmentCode = department.departCode;
            foreach (Department childDepartment in departmentMap.Values)
            {
                if (childDepartment.parentCode == parentDepartmentCode)
                {
                    childDepartmentList.Add(childDepartment);
                }
            }

            return childDepartmentList;
        }

        public Department getParentDepartment(Department department)
        {
            String parentDepartmentCode = department.parentCode;
            return getDepartmentByCode(parentDepartmentCode);
        }

        public int sync()
        {
            IList<Department> departmentList = departmentService.GetAll();

            if (departmentList.Count == 0)
            {
                log.Error("加载的组织信息为空！");
                return 0;
            }

            Dictionary<string, Department> departmentMap = new Dictionary<string, Department>();
            foreach(Department department in departmentList)
            {
                departmentMap.Add(department.departCode, department);
            }

            this.departmentMap = departmentMap;

            return departmentList.Count;
        }
    }
}
